package com.gsww.vue.controller.sys;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.gsww.vue.controller.BaseController;
import com.gsww.vue.entity.sys.SysAccount;
import com.gsww.vue.entity.sys.SysDepartment;
import com.gsww.vue.entity.sys.SysMenu;
import com.gsww.vue.entity.sys.SysRole;
import com.gsww.vue.service.sys.SysMenuService;
import com.gsww.vue.utils.Md5;
import com.gsww.vue.utils.StringHelper;
import com.gsww.vue.utils.TimeHelper;
/**
 * <p>Copyright: Copyright (c) 2014</p>
 * <p>公司名称 : 中国电信甘肃万维公司</p>
 * <p>项目名称 : jup-web</p>
 * <p>创建时间 : 2014-6-08 下午10:55:12</p>
 * <p>类描述 :   账号管理模块控制器    </p>
 *
 * @version 3.0.0
 * @author <a href=" ">zhangtb</a>
 */
@RestController
@RequestMapping(value = "/sys")
public class SysMenuController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(SysMenuController.class);
	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * 获取用户列表
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/menuList",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> menuList(@RequestParam String  pageNo,@RequestParam String  pageSize,
			HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		String menuName = request.getParameter("menuName");
		try{
			Map<String,Object> map = sysMenuService.getMenuList(menuName, null,pageNo,pageSize);
			//List<Map<String,Object>> list = (List<Map<String, Object>>) map.get("result");
			resMap.put("data", map);
			resMap.put("success", true);
		}catch(Exception ex){
			resMap.put("success", false); 
			ex.printStackTrace();
		}
		return resMap;
	}
	/**
	 * 返回菜单Tree
	 * 按照setid（帐套的物理id过滤）
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/menuTree", method = RequestMethod.GET)
	public Map<String,Object> departmenttree(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//String deptCode = request.getParameter("deptCode");
		try {
			//SysUserSession sysUserSession = (SysUserSession) request.getSession().getAttribute("sysUserSession");
			List<SysMenu> list = new ArrayList<SysMenu>(50);
			if(StringUtils.isBlank(id) ) {
				list = sysMenuService.findFirstMenu();
			} else {
				list = sysMenuService.findMenuByParentMenuId(id);
			}
			//System.out.println(id);
			List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
			if (list != null && !list.isEmpty()) {
				for (SysMenu sysMenu : list) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("id", sysMenu.getMenuId());
					map.put("pId", sysMenu.getParentMenuId());
					map.put("name", sysMenu.getMenuName());
					map.put("title", sysMenu.getMenuName());
					List<SysMenu> subList = sysMenuService.findMenuByParentMenuId(sysMenu.getMenuId());
					//System.out.println(subList.size()+"--"+subList.isEmpty());
					if(!subList.isEmpty()) {
						//System.out.println(sysDepartment.getDeptName());
						map.put("children", new ArrayList());
						map.put("loading", false);
						map.put("hasChildren", true);
					} else {
						map.put("hasChildren", false);
					}
					treeList.add(map);
				}
			}
			resMap.put("treeList", treeList);
			resMap.put("success", true);
		} catch (Exception e) {
			resMap.put("success", false);
			e.printStackTrace();
		}
		return resMap;
	}
	/**
	 * 保存菜单信息
	 */
	@RequestMapping(value = "/menuSave", method = RequestMethod.POST)
	//@RequestParam String  userAcctId,@RequestParam String  deptId, @RequestParam SysAccount sysAccount
	//@RequestBody Map<String,Object> params
	public Map<String,Object> accountSave(@RequestBody SysMenu  sysMenu ,HttpServletRequest request,HttpServletResponse response)  throws Exception {
		Map<String,Object> resMap = new HashMap<String,Object>();
		try {
			if(!StringHelper.isNotBlack(sysMenu.getMenuState())){
				sysMenu.setMenuState("1");
			}
			sysMenuService.save(sysMenu);
			resMap.put("success",true);
		} catch (Exception e) {
			resMap.put("success",false);
			e.printStackTrace();
		} 
		return resMap;
	}
	@RequestMapping(value = "/menuEdit", method = RequestMethod.GET)
	public Map<String,Object> menuEdit(@RequestParam String  menuId, HttpServletRequest request,HttpServletResponse response) {
		System.out.println(menuId);
		Map<String,Object> resMap = new HashMap<String,Object>();
		try {
			SysMenu sysMenu = null;
			if(StringHelper.isNotBlack(menuId)){
				sysMenu = sysMenuService.findByKey(menuId);
				SysMenu parentMenu = sysMenuService.findByKey(sysMenu.getParentMenuId());
				if(null != parentMenu){
					sysMenu.setParentMenuName(parentMenu.getMenuName());
				}
			}else{
				sysMenu = new SysMenu();
			}
			resMap.put("sysMenu", sysMenu);
			resMap.put("success", true);
		} catch (Exception e) {
			resMap.put("success", false);
			e.printStackTrace();
		}
		return resMap;
	}
	@RequestMapping(value = "/menuDelete", method = RequestMethod.GET)
	public Map<String,Object> menuDelete(@RequestParam String  menuId, HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		try {
			if(StringHelper.isNotBlack(menuId)){
				SysMenu sysMenu = sysMenuService.findByKey(menuId);
				sysMenu.setMenuState("0");
				sysMenuService.save(sysMenu);
			}
			resMap.put("success", true);
		} catch (Exception e) {
			resMap.put("success", false);
			e.printStackTrace();
		}
		return resMap;
	}
}
